<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="keyword">
        <el-input
            v-model="queryParams.keyword"
            placeholder="请输入用户账号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column width="80" label="用户id" align="center" prop="loginId"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="主机" align="center" prop="ipaddr"/>
      <el-table-column label="登陆地点" align="center" prop="loginLocation"/>
      <el-table-column label="操作系统" align="center" prop="os"/>
      <el-table-column label="浏览器" align="center" prop="browser"/>
      <el-table-column label="登录时间" align="center" prop="loginTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['forum:online:kick']">踢下
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page="queryParams.pageNum"
        :limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="ForumUser">
import {listUser, kickUser} from "@/api/forum/userOnline.js";

const {proxy} = getCurrentInstance();

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    keyword: null
  }
});

const {queryParams, form} = toRefs(data);

/** 查询论坛用户信息列表 */
function getList() {
  loading.value = true;
  listUser(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.loginId || ids.value;
  proxy.$modal.confirm('是否确认踢下论坛用户信息编号为"' + _userIds + '"的数据项？').then(function () {
    return kickUser(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("踢下成功");
  }).catch(() => {
  });
}


getList();
</script>
